# coding=utf-8

from functools import partial
from qtpy import QtWidgets, QtCore
from .ui.thumbnail_item_row_widget import Ui_thumbnail_item_row_widget
class ThumbnailItemBaseWidget(QtWidgets.QWidget):
    def __init__(self, widget_factory, parent):
        QtWidgets.QWidget.__init__(self, parent)
        self._ui = widget_factory()
        self._ui.setupUi(self)
        self._ui.thumbnail.setScaledContents(False)

        # self._ui.box.setProperty('item_type', 'thumbnail')
        self._ui.box.setProperty('item_status', 'normal')
        

        # 设置菜单
        self._menu = QtWidgets.QMenu(self)
        self._actions = []
        # self._ui.action_btn.setMenu(self._menu)
        self._ui.action_btn.setVisible(False)
        # self._menu.setStyleSheet("""QMenu{
        #                                 color:rgb(200,200,200);
        #                                 font: 9pt \"Arial\";
        #                                 background:rgb(35,35,35);
        #                                 border: 1px solid rgb(0, 120, 215);
        #                                 }
        #                             QMenu:item:disabled{
        #                                 color:rgba(50,50,50);
        #                                 }
        #                             QMenu:item:disabled:selected{
        #                                 background:rgba(0, 120, 215, 30);
        #                                 color:rgba(50,50,50);
        #                                 }
        #                             QMenu:item:selected{
        #                                 background:rgb(0, 120, 215);
        #                                 }

        #                                 """)
    def menu_enable(self, status):
        if status:
            self._ui.action_btn.setMenu(self._menu)
        else:
            self._ui.action_btn.setMenu(None)
    def set_actions(self, actions, hook, data):
        """
        添加菜单
        """
        self._actions = actions
        name_list = self._actions.keys()
        for name in name_list:
            cmd = self._actions[name]
            action = QtWidgets.QAction(name, self)
            action.triggered.connect(partial(hook.main, cmd, data))
            self._menu.addAction(action)
            
    def set_border_radius(self, status):
        self._ui.box.setProperty('border_radius', status)

    def set_action_btn_visible(self, is_visible):
        """
        菜单按钮的显示
        """
        self._ui.action_btn.setVisible(is_visible)

    def set_thumbnail(self, pixmap):
        """
        设置预览图片
        """
        self._ui.thumbnail.setPixmap(pixmap)

    def set_status(self, status='normal'):
        self._ui.box.setProperty('item_status', status)
        self._ui.box.style().polish(self._ui.box)
        self._ui.box.update()

    @staticmethod
    def calculate_size(width, height):
        return QtCore.QSize(width, height)

    def set_selected(self, value):
        if value:
            self.set_status('selected')
        else:
            self.set_status('normal')

